package com.evan.review.anr;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.evan.review.R;

/**
 * ANR(应用无响应)示例的主Activity
 * 提供了不同ANR触发方式的入口按钮
 */
public class ANRMainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnMainThreadBlock; // 主线程阻塞ANR按钮
    private Button btnDeadlock; // 死锁ANR按钮
    private Button btnServiceTimeout; // Service超时ANR按钮
    private Button btnBroadcastTimeout; // 广播超时ANR按钮
    private Button btnInputDispatchTimeout; // 输入事件分发超时ANR按钮
    private Button btnLowMemory; // 内存紧张ANR按钮
    private Button btnComparison; // 错误与正确案例对比按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anr_main);
        
        initViews(); // 初始化视图
        setListeners(); // 设置监听器
    }

    /**
     * 初始化视图组件
     */
    private void initViews() {
        btnMainThreadBlock = findViewById(R.id.btn_main_thread_block);
        btnDeadlock = findViewById(R.id.btn_deadlock);
        btnServiceTimeout = findViewById(R.id.btn_service_timeout);
        btnBroadcastTimeout = findViewById(R.id.btn_broadcast_timeout);
        btnInputDispatchTimeout = findViewById(R.id.btn_input_dispatch_timeout);
        btnLowMemory = findViewById(R.id.btn_low_memory);
        btnComparison = findViewById(R.id.btn_comparison);
    }

    /**
     * 设置按钮监听器
     */
    private void setListeners() {
        btnMainThreadBlock.setOnClickListener(this);
        btnDeadlock.setOnClickListener(this);
        btnServiceTimeout.setOnClickListener(this);
        btnBroadcastTimeout.setOnClickListener(this);
        btnInputDispatchTimeout.setOnClickListener(this);
        btnLowMemory.setOnClickListener(this);
        btnComparison.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        
        if (id == R.id.btn_main_thread_block) {
            // 跳转到主线程阻塞ANR示例
            startActivity(new Intent(this, MainThreadBlockActivity.class));
        } else if (id == R.id.btn_deadlock) {
            // 跳转到死锁ANR示例
            startActivity(new Intent(this, DeadlockActivity.class));
        } else if (id == R.id.btn_service_timeout) {
            // 跳转到Service超时ANR示例
            startActivity(new Intent(this, ServiceTimeoutActivity.class));
        } else if (id == R.id.btn_broadcast_timeout) {
            // 跳转到广播超时ANR示例
            startActivity(new Intent(this, BroadcastTimeoutActivity.class));
        } else if (id == R.id.btn_input_dispatch_timeout) {
            // 跳转到输入事件分发超时ANR示例
            startActivity(new Intent(this, InputDispatchTimeoutActivity.class));
        } else if (id == R.id.btn_low_memory) {
            // 跳转到内存紧张ANR示例
            startActivity(new Intent(this, LowMemoryActivity.class));
        } else if (id == R.id.btn_comparison) {
            // 跳转到错误与正确案例对比界面
            startActivity(new Intent(this, ANRComparisonActivity.class));
        }
    }
} 